package leetcom.Lccup21;

import java.util.Arrays;

public class EX2 {
    public int orchestraLayout(int num, int xPos, int yPos) {
        long up = 0;
        long down = num-1;
        long left = 0;
        long right = num-1;
        long now=1;

        int flag=0;
        if(num==1){
            return 1;
        }
        while(true){
            if(up == xPos){
                long l = now + (yPos - left) % 9;
                return (int)(l>9?l%9:l);
            }
            now = now+(right-left)%9;
            now = now>9?now%9:now;
            if(flag!=0){
                left++;
            }
            if(yPos==right){
                long l = now+(xPos-up)%9;
                return (int)(l>9?l%9:l);
            }
            now = now+(down-up)%9;
            now = now>9?now%9:now;
            up++;
            if(down== xPos){
                long l = now+(right-yPos)%9;
                return (int)(l>9?l%9:l);

            }
            now = now+(right-left)%9;
            now = now>9?now%9:now;
            right--;
            if(left == yPos){
                long l = now+(down-xPos)%9;
                return (int)(l>9?l%9:l);
            }
            now = now+(down-up)%9;
            now = now>9?now%9:now;
            down--;
            flag++;
        }
    }

    public static void main(String[] args) {
        EX2 ex2 = new EX2();
        ex2.orchestraLayout(4,1,2);
    }

}
